package 读写锁ReadWriteLock;

import java.util.concurrent.locks.*;

public class Demo {
    static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    static ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
    static ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

    public void read(Lock lock) throws InterruptedException {
        lock.lock();
        Thread.sleep(500);
        lock.unlock();
    }

    public static void main(String[] args) throws InterruptedException {
        Demo demo = new Demo();
//        long start = System.currentTimeMillis();

        for (int i = 0; i < 400; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        demo.read(readLock);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
        }
//        long end = System.currentTimeMillis();
//        System.out.println((end - start) / 1000 + "秒");// 2S
    }
    // 顺势读完，因为所有读操作不冲突，相当于一个读操作的时间
}
